package com.baigt.j8.stream;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

import static java.util.stream.Collector.Characteristics.*;

public class StreamCustomCollector {
    public static void main(String[] args) {
        Set<String> sets = Arrays.asList("ricky", "baigt", "weiyi").stream().collect(new CustomCollector<String>());
        sets.forEach(System.out::println);
    }
}
class CustomCollector<E> implements Collector<E,Set<E>, Set<E>>{

    @Override
    public Supplier<Set<E>> supplier() {
        System.out.println("supplier invoke");
        return HashSet::new;
    }

    @Override
    public BiConsumer<Set<E>, E> accumulator() {
        System.out.println("accumulator invoke ");
        return (sets,e)->{sets.add(e);};
    }

    @Override
    public BinaryOperator<Set<E>> combiner() {
        System.out.println("combiner");
        return (sets1,sets2)->{sets1.addAll(sets2);
            return sets1;
        };
    }

    @Override
    public Function<Set<E>, Set<E>> finisher() {
        System.out.println("finisher invoke");
        return Function.identity();
    }

    @Override
    public Set<Characteristics> characteristics() {
        System.out.println("characterisstics invoke");
        return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH,UNORDERED,CONCURRENT));
    }
}
